package com.dl.mall.service.admin.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dl.mall.entity.Page;
import com.dl.mall.entity.admin.Admin;
import com.dl.mall.entity.user.User;
import com.dl.mall.mapper.AdminMapper;
import com.dl.mall.mapper.UserMapper;
import com.dl.mall.service.admin.UserService_Admin;
import com.dl.mall.utils.PageUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author DL
 * @create 2022-04-09 17:46
 */
@Service
@Transactional
public class UserServiceImpl_Admin implements UserService_Admin {

    @Resource
    private UserMapper userMapper;

    @Override
    public boolean modifyState(String userId, int state) {
        return userMapper.updateState(userId, state) != 0;
    }

    @Override
    public boolean modifyUserInfo(User user) {
        return userMapper.updateUserInfo(user) != 0;
    }

    @Override
    public String resetPassword(String userId) {
        String md5BySalt = SaSecureUtil.md5BySalt("123", userId);

        if (userMapper.updatePassword(userId, md5BySalt) != 0){
            return md5BySalt;
        }
        return null;
    }

    @Override
    public User getUserByUserId(String userId) {
        return userMapper.queryUserByUserId(userId);
    }

    /**
     * 根据 条件 查询用户列表
     * @param condition 条件
     * @return 用户分页
     */
    @Override
    public Page<User> getUserByCondition(JSONObject condition) {
        PageHelper.startPage(condition.getInteger("currentPage"), 10);

        List<User> userList = userMapper.queryUserByCondition(condition);

        return (Page<User>) PageUtils.getPage(userList);
    }



    /* ===========admin======== */

}
